<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Http\Requests;

use DB;

class NoticeController extends Controller
{
    //公告页面
    public function index(Request $request)
    {
        // 保存搜索的条件
        $where = [];
        $db = DB::table('storenotice');

        if($request->has('title')){
            $db->where('title','like',"%{$request->input('title')}%");
            $where['title'] = $request->input('title');
        }
        $list = $db->paginate(2);
        
        // 原生语句
        return view('admin.Notice',['list'=>$list,'where'=>$where]);
    }

    public function create()
    {
        return view('admin.noticeAdd');
    }

    public function store(Request $request)
    {    
        // 获取发表时间，去除token  
        $data = $request->except('_token');
        $data['updateTime'] = date('Y-m-d H:i:s');
        $row = DB::table('storenotice')->insert($data);
        if($row>0){
            return redirect('/admin/Notice')->with('msg','添加成功');
        }

    }

    public function edit($id)
    {
        $value = DB::table('storenotice')->where('id',$id)->first();
        return view('admin.noticeEdit',['v'=>$value]);
    }

    public function update($id,Request $request)
    {
        // 获取除了指定字段，剩下的字段
        $data = $request->except('_token','_method');
        $data['updateTime'] = date('Y-m-d H:i:s');
        $row = DB::table('storenotice')->where('id',$id)->update($data);
        if($row>0){
            return redirect('/admin/Notice')->with('msg','修改成功');
        }
    }

    public function destroy($id)
    {

        $row = DB::table('storenotice')->where('id',$id)->delete();
        if($row>0){
            return redirect('/admin/Notice')->with('msg','删除成功');
        }
    }
}